package per.hx.bs.sys.serviceimpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.domain.PageDO;
import per.hx.bs.base.entity.BsLogDO;
import per.hx.bs.sys.dao.SysLogDao;
import per.hx.bs.util.Query;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/3/26  17:08
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "sys-service/log")
public class SysLogApiServiceImpl {

    private final
    SysLogDao sysLogDao;

    @Autowired
    public SysLogApiServiceImpl(SysLogDao sysLogDao) {
        this.sysLogDao = sysLogDao;
    }

    /**
     * 日志出插入
     *
     * @param bsLogDO 日志信息
     * @return 影响行数
     */
    @PostMapping(value = "/save")
    public int save(@RequestBody BsLogDO bsLogDO) {
        return sysLogDao.insert(bsLogDO);
    }

    /**
     * 按条件获取日志列表
     *
     * @param map 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/list")
    public PageDO<BsLogDO> selectList(@RequestBody Map<String, Object> map) {
        Query query = new Query(map);
        int total = sysLogDao.count(query);
        List<BsLogDO> logDOS = sysLogDao.list(query);
        PageDO<BsLogDO> page = new PageDO<>();
        page.setTotal(total);
        page.setRows(logDOS);
        return page;
    }

    /**
     * 删除日志
     *
     * @param id 日志Id
     * @return 影响的行数
     */
    @DeleteMapping(value = "/delete")
    public int delete(@RequestParam("id") Long id) {
        return sysLogDao.deleteById(id);
    }

    /**
     * 批量 删除 日志
     *
     * @param ids 日志ids
     * @return 影响的行数
     */
    @DeleteMapping(value = "/batchDelete")
    public int batchDelete(@RequestParam("ids") Long[] ids) {
        return sysLogDao.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 清空所有日志
     *
     * @return 影响的行数
     */
    @DeleteMapping(value = "/cleanAll")
    public int cleanAll() {
        EntityWrapper<BsLogDO> ew = new EntityWrapper<BsLogDO>();
        ew.setEntity(new BsLogDO());
        ew.where("id is not null");
        return sysLogDao.delete(ew);
    }

}
